/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package acceso.servlet;

import wf.dto.qbfrawor_id_frawor;
import acceso.hibernate.AccesoHibernate;
import acceso.util.ContenedorLink;
import acceso.util.Escritor;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.hibernate.Query;
import org.hibernate.StatelessSession;

/**
 *
 * @author john
 */
public class WorkflowLink extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        HttpSession session = request.getSession();
        StatelessSession HSESSION = null;
        int ID_FRAWOR = 0;
        
        try {
            Escritor.escribe_frawor("===== Iniciando Workflow-Link ======");
            
            HSESSION = AccesoHibernate.getSessionFactory().openStatelessSession();
            //GENERACION DEl ID_FRAWOR 
            try{
                Query HQUERY=HSESSION.getNamedQuery("frawor2.qbfrawor_id_frawor");
                HQUERY.setString("frawor", "fratra.qbfrawor_id_frawor");
                Escritor.escribe_transa(HQUERY.getQueryString().replaceAll(":frawor", "\'fratra.qbfrawor_id_frawor\'"));
                qbfrawor_id_frawor qbfrawor = (qbfrawor_id_frawor) HQUERY.uniqueResult();
                ID_FRAWOR = qbfrawor.getNextval();
            }catch(Exception ep){
                throw ep;
            } 
            
            String url = request.getQueryString();
            url = url.substring(url.indexOf("=")+1);
            
            ContenedorLink cl = new ContenedorLink();
            cl.setUrl(url);

            session.setAttribute("c"+ID_FRAWOR, cl);
            response.sendRedirect(request.getContextPath()+"/workflow2/contenedor.jsp?id_frawor="+ID_FRAWOR);

                    
        }catch(Exception e){
            Escritor.escribe_errors("[W]ep:" + e.getClass() + "::" + e.getMessage());
        }finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
